#pragma once
#include<iostream>
#include<fcntl.h>
#include<string>
#include<pthread.h>
#include<ctime>
#include<unistd.h>
#include<cstdlib>
#include<cstdio>
#include<sys/types.h>
#include<cstring>
#include<cstdarg>
using namespace std;
const string filename="log/tcpServer.log";
enum
{
  DEBUG=1,
  INFO,
  WARING,
  ERROR,
  FATAL,
  UNKONW
};  

string To_string_grade(int grade)
{
    switch(grade)
    {
      case 1:
      return "DEBUG";
      case 2:
      return "INFO";
      case 3:
      return "WARING";
      case 4:
      return "ERROR";
      case 5:
      return "FATAL";
      default:
      return "UNKONW";
    }
}
void logMessge(int grade,const char* format,...)
{
  char left[2048]={0};
  char right[1024]={0};
  string stringGrade=To_string_grade(grade);
  time_t ti=time(nullptr);
  auto tm=localtime(&ti);
  snprintf(left,sizeof(left),"[%d-%d-%d %d:%d:%d] %s %d",tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,stringGrade.c_str(),getpid()   );
  va_list args;
  va_start(args,format);
  vsnprintf(right,sizeof(right),format,args);
  va_end(args);

  printf("%s%s\n",left,right);
  // FILE*fp=fopen(filename.c_str(),"a");
  // if(fp==nullptr)
  //   return;
  // fprintf(fp,"%s%s",left,right);
  // fflush(fp);
  // fclose(fp);
  
} 
